package com.wondertek.poms.service.controller;

import cn.hutool.core.convert.Convert;
import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IProductInfoPackageService;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.pojo.ProductInfoPackagePojo;
import com.wondertek.poms.service.vo.ProductInfoPackageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * <p><b>ProductInfoPackageController Description:</b> 二级产品信息 Controller层实现</p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-19 15:56:40</p>
 */
@Api(value = "ProductInfoPackageController", description = "二级产品 接口")
@RestController
@RequestMapping(value = "/product-info-package")
@Validated
public class ProductInfoPackageController extends BaseController {

	@Autowired
	private IProductInfoPackageService productInfoPackageService;


	@ApiOperation(value = "二级产品信息 列表", notes = "二级产品信息 列表")
	@GetMapping(value = "/list")
	@ResponseBody
	public ResultBean list(
			ProductInfoPackageVo productInfoPackageVo,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int rows,
			@RequestParam(defaultValue = "LAST_UPDATE_DATE") String sidx,
			@RequestParam(defaultValue = "desc") String sord) {
		ProductInfoPackagePojo productInfoPackagePojo = new ProductInfoPackagePojo();
		BeanUtils.copyProperties(productInfoPackageVo, productInfoPackagePojo);
		List<ProductInfoPackagePojo> pages = productInfoPackageService
				.findAll(productInfoPackagePojo, page, rows, sord, sidx);

		return ResultBean.ok(pages);
	}

	@ApiOperation(value = "二级产品信息 新增", notes = "二级产品信息 新增")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productInfoPackageName", required = true, value = "二级产品名"),
			@ApiImplicitParam(name = "firstProductInfoPackageId", required = true, value = "一级产品id"),
			@ApiImplicitParam(name = "type", required = true, value = "类型:大屏:bigScreen，小屏:smallScreen，VR:VR")
	})
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean add(@RequestBody ProductInfoPackageVo productInfoPackageVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(productInfoPackageVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}

		if (isRepeat(productInfoPackageVo)) {
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		}

		ProductInfoPackage productInfoPackage = new ProductInfoPackage();
		BeanUtils.copyProperties(productInfoPackageVo, productInfoPackage);
		productInfoPackage.setType(productInfoPackageVo.getType().toString());
		//productInfoPackage.setFirstProductInfoPackageId(Convert.toLong(productInfoPackageVo.getFirstProductInfoPackageId()));
		ProductInfoPackage.putTimeAndUser(productInfoPackage);
		productInfoPackageService.save(productInfoPackage);
		return ResultBean.ok(productInfoPackage);
	}

	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断二级产品信息 名称是否重复</p>
	 *
	 * @param productInfoPackageVo
	 * @author douzi
	 */
	private boolean isRepeat(ProductInfoPackageVo productInfoPackageVo) {
		ProductInfoPackage temp = new ProductInfoPackage();
		temp.setProductInfoPackageName(productInfoPackageVo.getProductInfoPackageName());
		Optional<ProductInfoPackage> productInfoPackageOpt = productInfoPackageService.findOne(Example.of(temp));
		if (productInfoPackageOpt.isPresent()) {
			ProductInfoPackage productInfoPackage = productInfoPackageOpt.get();
			if (productInfoPackageVo.getProductInfoPackageId() != null && !productInfoPackageVo.getProductInfoPackageId() .equals( productInfoPackage.getProductInfoPackageId())) {
				return true;
			} else if (productInfoPackageVo.getProductInfoPackageId() == null) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 二级产品用于下拉框
	 *
	 * @return 全量二级产品
	 */
	@GetMapping("all")
	public ResultBean all() {
		List<ProductInfoPackage> all = productInfoPackageService.findAll();
		return ResultBean.ok(all);
	}

	@ApiOperation(value = "二级产品信息 列表", notes = "二级产品信息 列表")
	@GetMapping(value = "/list-by-first-Prd-info")
	@ResponseBody
	public ResultBean getProductInfoPackageList(String firstProductId) {
		if(StringUtils.isBlank(firstProductId)){
			return ResultBean.error("1级产品id不能为空");
		}
		List<ProductInfoPackage> list = productInfoPackageService.findPrdInfoPackageByFirstPrd(Long.parseLong(firstProductId));
		return ResultBean.ok(list);
	}

}
